//
//  ESTActivityChartsViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/16.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTActivityChartsViewController.h"
#import "ESTPickerTipView.h"
#import "ESTActivityModel.h"
#import "ESTActivityEffectiveView.h"
#import "ESTChartsTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "ESTActivityChartsModel.h"
@interface ESTActivityChartsViewController ()<UITableViewDelegate, UITableViewDataSource>
/**
 *  表头
 */
@property(nonatomic, strong) NSArray<NSString *> *headerTexts;
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  排序类型
    1,邀约
    2,诚意金
    3,订单
 */
@property(nonatomic, assign) NSInteger chartsType;
/**
 *  排序类型
    0,数量
    1,转换率
 */
@property(nonatomic, assign) NSInteger chartsMode;
/**
 *  数据源
 */
@property(nonatomic, strong) NSArray<ESTActivityChartsModel *> *dataArray;
/**
 *  记录是否没有数据的状态
 */
@property(nonatomic, assign) BOOL isNoData;
@end

@implementation ESTActivityChartsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainTitle = [@"排行榜" S];
    [self setupActivityPickerView];
    [self setupChartsHeader];
    [self setupTableView];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.chartsType = 1;
}
#pragma mark - 设置UI
- (void)setupActivityPickerView {
    TYZView *headerView = [[TYZView alloc] init];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(30);
    }];
    headerView.backgroundColor = TYZWhiteColor;
    NSString *name = self.modelArray[self.number].ACTIVITY_NAME;
    if (!name) name = [@"选择活动" S];
    ESTPickerTipView *tipView = [[ESTPickerTipView alloc] init];
    tipView.title = name;
    tipView.textColor = PickerTipViewTextColor_333;
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.modelArray.count];
    [self.modelArray enumerateObjectsUsingBlock:^(ESTActivityModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [arrM addObject:obj.ACTIVITY_NAME];
    }];
    tipView.titleLabel.font = [UIFont systemFontOfSize:CoreInfo.fontSize];
    tipView.dataArray = [arrM mutableCopy];
    tipView.pickerTitle = [@"选择活动" S];
    tipView.selectedValue = @[name];
    [headerView addSubview:tipView];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(10);
        make.left.equalTo(headerView).offset(10);
    }];
    WEAKSELF
    __weak typeof(tipView) weakTipView = tipView;
    [tipView setDidChooseNumberBlock:^(NSArray<NSNumber *> *numberArray) {
        weakSelf.number = [numberArray.lastObject integerValue];
        [weakSelf refreshData];
        NSString *str = weakSelf.modelArray[weakSelf.number].ACTIVITY_NAME;
        weakTipView.title = str;
        weakTipView.selectedValue = @[str];
    }];
}
- (void)setupChartsHeader {
    TYZView *headerView = [[TYZView alloc] init];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    headerView.backgroundColor = TYZWhiteColor;
    TYZView *chartsHeaderView = [[TYZView alloc] init];
    [headerView addSubview:chartsHeaderView];
    [chartsHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(kWindowWidth * 0.8);
        make.top.bottom.equalTo(headerView);
    }];
    CGFloat width = kWindowWidth * 0.8 / 3;
    ESTActivityEffectiveView *childView1 = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:self.headerTexts[0]];
    [chartsHeaderView addSubview:childView1];
    [childView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(chartsHeaderView);
        make.left.equalTo(chartsHeaderView);
        make.width.mas_equalTo(width);
    }];
    childView1.isSelected = YES;
    ESTActivityEffectiveView *childView2 = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:self.headerTexts[1]];
    [chartsHeaderView addSubview:childView2];
    [childView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(chartsHeaderView);
        make.centerX.equalTo(chartsHeaderView);
        make.width.mas_equalTo(width);
    }];
    ESTActivityEffectiveView *childView3 = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:self.headerTexts[2]];
    [chartsHeaderView addSubview:childView3];
    [childView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(chartsHeaderView);
        make.right.equalTo(chartsHeaderView);
        make.width.mas_equalTo(width);
    }];
    WEAKSELF
    __weak typeof(childView1) weakChildView1 = childView1;
    __weak typeof(childView2) weakChildView2 = childView2;
    __weak typeof(childView3) weakChildView3 = childView3;
    [childView1 setDidCliclCoverBlock:^{
        weakChildView1.isSelected = YES;
        weakChildView2.isSelected = NO;
        weakChildView3.isSelected = NO;
        weakSelf.chartsType = 1;
    }];
    [childView2 setDidCliclCoverBlock:^{
        weakChildView1.isSelected = NO;
        weakChildView2.isSelected = YES;
        weakChildView3.isSelected = NO;
        weakSelf.chartsType = 2;
    }];
    [childView3 setDidCliclCoverBlock:^{
        weakChildView1.isSelected = NO;
        weakChildView2.isSelected = NO;
        weakChildView3.isSelected = YES;
        weakSelf.chartsType = 3;
    }];
}
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(80);
    }];
    self.tableView.estimatedRowHeight = 100;
    [self setupTableViewHeader];
}
- (void)setupTableViewHeader {
    WEAKSELF
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [weakSelf refreshData];
    }];
}
#pragma mark - actions
- (void)refreshData {
    [self acquireActivity];
    [self acquireData];
}
- (void)acquireActivity {
    WEAKSELF
    [ESTActivityModel requestAllActivitySuccess:^(NSArray<ESTActivityModel *> *modelArray) {
        weakSelf.modelArray = modelArray;
    } failure:^(NSString *error) {
        
    }];
}
- (void)acquireData {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTActivityChartsModel requestActivityChartsDataWithActivityID:self.modelArray[self.number].ACTIVITY_ID bus_type:self.chartsType orderby:self.chartsMode success:^(NSArray<ESTActivityChartsModel *> *modelArray) {
        [TYZLoadingView hideHUD];
        weakSelf.dataArray = modelArray;
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *error) {
        [TYZLoadingView showError:NetworkUnusual];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, kWindowWidth*0.5, 0, kWindowWidth*0.5)];
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, kWindowWidth*0.5, 0, kWindowWidth*0.5)];
        }
    } else {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 20;
    }
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        TYZTableViewCell *cell = [[TYZTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityChartsViewController"];
        TYZLabel *label = [TYZLabel labelWithText:[@"转换率=业绩数÷跟进客户数×100%" S]];
        label.font = [UIFont systemFontOfSize:CoreInfo.fontSize - 4];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = TYZBlackColor;
        [cell.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell.contentView);
        }];
        return cell;
    }
    ESTChartsTableViewCell *cell = [ESTChartsTableViewCell creatChartsTableViewCell:tableView selectedNumber:self.chartsMode];
    if (indexPath.row == 1) {
        cell.firstTitle = [@"排名" S];
        cell.secondTitle = [@"名字" S];
        cell.thirdTitle = [@"数量" S];
        cell.fourthTitle = [@"转换率" S];
        WEAKSELF
        [cell setDidClickNumberBlock:^{
            weakSelf.chartsMode = 0;
        }];
        [cell setDidClickPercentageBlock:^{
            weakSelf.chartsMode = 1;
        }];
    } else {
        cell.firstTitle = [NSString stringWithFormat:@"%ld", (long)indexPath.row - 1];
        cell.model = self.dataArray[indexPath.row - 2];
    }
    return cell;
}
#pragma mark - setter & getter
- (NSArray<NSString *> *)headerTexts {
    if (_headerTexts == nil) {
        _headerTexts = @[
                         [@"邀约" S],
                         [@"诚意金" S],
                         [@"订车" S]
                         ];
    }
    return _headerTexts;
}
- (TYZTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TYZTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)setChartsType:(NSInteger)chartsType {
    _chartsType = chartsType;
    [self refreshData];
}
- (void)setChartsMode:(NSInteger)chartsMode {
    _chartsMode = chartsMode;
    [self refreshData];
}
- (void)setDataArray:(NSArray<ESTActivityChartsModel *> *)dataArray {
    _dataArray = dataArray;
    if (dataArray.count > 0) {
        if (self.isNoData) {
            self.isNoData = NO;
            [self setupTableView];
        } else {
            [self.tableView reloadData];
        }
    } else {
        self.isNoData = YES;
        [self.tableView removeFromSuperview];
        [self.view noDataWithClick:^{
            [self refreshData];
        }];
    }
}
@end
